package com.sdry.web.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.sdry.base.BaseController;
import com.sdry.entity.CheckDetail;
import com.sdry.entity.CheckDetailExample;
import com.sdry.entity.CheckDetailExample.Criteria;
import com.sdry.entity.PartComponent;
import com.sdry.entity.PartStore;
import com.sdry.entity.PartStoreExample;
import com.sdry.service.CheckDetailServise;
import com.sdry.service.PartStoreServise;
import com.sdry.utils.ResponseUtil;

/**
 * @ClassName: CheckDetailController
 * @Description: 盘点单详情
 * @author jyy
 * @date 2018年7月26日
 * @version V1.0
 */
@Controller
@RequestMapping("/checkDetail")
public class CheckDetailController extends BaseController{

    @Resource
    private CheckDetailServise checkDetailServise;
    @Resource
    private PartStoreServise partStoreServise;


    /**
     * 添加借调管理
     * @param response 响应
     * @param CheckDetail 盘点单实体
     * @throws Exception 异常
     */
    @RequestMapping("/insertCheckDetail")
    public void insertCheckDetail( HttpServletResponse response,
    		String[] parCid, String[] acount, String[] scount, String sid) throws Exception {
    	int affact = 0;
    	 if(parCid.length > 0) {
    		 for (int i = 0; i < parCid.length; i++) {
        		 CheckDetail checkDetail = new CheckDetail(); 
        		 checkDetail.setSid(sid);
        		 checkDetail.setParCid(parCid[i]);
        		 //账面库存
        		 checkDetail.setScount(scount[i]);
        		 //实际库存
        		 checkDetail.setAcount(acount[i]);
        		 
        		 Integer a = Integer.valueOf(acount[i]);
        		 Integer s = Integer.valueOf(scount[i]);
        		 Integer d = a - s;
        		 checkDetail.setDiffcount(d+"");
        		 //添加
        	     checkDetailServise.insert(checkDetail);
        	     checkDetail = null;
    		}
    		affact = 1;
    	 }
        ResponseUtil.write(response, affact);
    }

    /**
     * 带条件的分页查询盘点单信息
     * @param request 请求
     * @param response 响应
     * @param limit 每页记录数
     * @param offset 页面
     * @param CheckDetail 盘点单信息实体
     * @throws Exception 异常
     */
    @RequestMapping("/selectCheckDetailList")
    public void selectCheckDetailList(HttpServletRequest request, HttpServletResponse response,
                                   @RequestParam(value = "limit",required = false)int limit,
                                   @RequestParam(value = "offset",required = false)int offset,
                                   CheckDetail checkDetail) throws Exception {
        int x = 1;
        CheckDetailExample example = new CheckDetailExample();
        
		//统计
        int count = checkDetailServise.countByExample(example );
        //分页查询
        List<CheckDetail> list = checkDetailServise.selectByExampleForStartPage(example, offset-x, limit);

        JSONObject jsonObject=new JSONObject();
        jsonObject.put("total", count);
        jsonObject.put("rows", list);
        ResponseUtil.write(response, jsonObject);
    }

    /**
     * 根据id查询盘点单信息
     * @param response 响应
     * @param id 盘点单id
     * @throws Exception 异常
     */
    @RequestMapping("/selectCheckDetailById")
    public void selectCheckDetailById(HttpServletResponse response, Long id) throws Exception {

        CheckDetail checkDetail = checkDetailServise.selectByPrimaryKey(id);
        //将对象转化为json
        JSONObject json = JSONObject.fromObject(checkDetail);
        ResponseUtil.write(response, json);
    }

    /**
     * 修改盘点单信息
     * @param response 响应
     * @param CheckDetail 盘点单信息
     * @throws Exception 异常
     */
    @RequestMapping("/editCheckDetail")
    public void editCheckDetail( HttpServletResponse response,
                             CheckDetail checkDetail) throws Exception {

        //修改
        int affct = checkDetailServise.updateByPrimaryKey(checkDetail);
        ResponseUtil.write(response, affct);
    }

    /**
     * 批量删除盘点单
     * @param response 响应
     * @param ids 盘点单id
     * @throws Exception 异常
     */
    @RequestMapping("/deleteCheckDetail")
    public void deleteCheckDetail( HttpServletResponse response, String ids) throws Exception {

        int affact =checkDetailServise.deleteByPrimaryKeys(ids);
        ResponseUtil.write(response, affact);
    }
    
    /**
     * 查询所有库存
     * @param response 响应
     * @throws Exception 异常
     */
    @RequestMapping("/selectAllStock")
    public void selectAllStock(HttpServletResponse response) throws Exception {
    	PartStoreExample example = new PartStoreExample();
		List<PartStore> partStores = partStoreServise.selectByExample(example );
		
		JSONArray array=JSONArray.fromObject(partStores);
        ResponseUtil.write(response, array);
    }
    
    
    /**
     * 根据盘点单号查询盘点详情
     * @throws Exception 
     */
    @RequestMapping("/selectBySid")
    public void selectBySid(String sid,HttpServletResponse response) throws Exception {
    	CheckDetailExample example = new CheckDetailExample();
    	Criteria createCriteria = example.createCriteria();
    	
    	if(sid != null && sid != "") {
    		createCriteria.andSidEqualTo(sid);
    	}
    	
    	List<CheckDetail> list = checkDetailServise.selectByExample(example );
    	JSONObject jsonObject=new JSONObject();
        /*jsonObject.put("total", count);*/
        jsonObject.put("rows", list);
        ResponseUtil.write(response, jsonObject);
    }
    
    /**
     * 根据仓库统计库存信息
     * @param area 仓库id
     * @throws Exception 异常
     */
    @RequestMapping("/selectComByArea")
    public void selectComByArea(String area, HttpServletResponse response) throws Exception {
    	List<PartComponent> list= checkDetailServise.selectComByArea(area);
    	JSONObject jsonObject=new JSONObject();
        /*jsonObject.put("total", count);*/
        jsonObject.put("rows", list);
        ResponseUtil.write(response, jsonObject);
    }

}
